﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RayDen.Library.Components.SystemComponents;

namespace RayDen.RdfViewer.Controllers
{
    public class LogController
    {
        private StringCollection logData;

        public LogController(TextBox tx)
        {
            this.logData = new StringCollection();
            TracerFactory.Instance.Populate(new TextBoxTracer(tx, logData));
        }

        public class TextBoxTracer : ITracerImplementation
        {
            protected TextBox txtLog;
            protected Action<string> writeAction;
            private StringCollection logData;

            public TextBoxTracer(TextBox t, StringCollection data)
            {
                this.txtLog = t;
                this.logData = data;
                writeAction = (s) => { t.Text += s; };
            }

            public void Write(string template, params object[] parameters)
            {
                this.WriteToBox(string.Format(template, parameters));
            }

            public void WriteLine(string template, params object[] parameters)
            {
                this.WriteToBox(string.Format(template, parameters) + Environment.NewLine);
            }

            private void WriteToBox(string msg)
            {
                this.writeAction.Invoke(msg);
                logData.Add(msg);
            }
        }
    }
}
